﻿/* Commenting out in 3.5 Version
namespace DirectoryServicesProtocolsExtensions.SearchResult
{
    using System.Collections.Generic;
    using System.DirectoryServices.Protocols;
    using System.Dynamic;

    using DirectoryServicesProtocolsExtensions.LdapConnection;

    public static class SearchResultToExapndoObject 
    {
        public static dynamic ToExpandoObject(
           this SearchResultEntry searchResultEntry,
           LdapConnection ldapConnection)
        {
            if (searchResultEntry.Attributes.Values == null)
            {
                return null;
            }

            var searchResultParser = new SearchResultParser(ldapConnection);

            dynamic expando = new ExpandoObject();
            var expandoDictionary = expando as IDictionary<string, object>;
            
            foreach (DirectoryAttribute attribute in searchResultEntry.Attributes.Values)
            {
                var ldapToDotNetMetaMap = ldapConnection.GetAttributeMetaData(
                    SearchResultParser.StripExpansionDirectiveFromAttributeNameIfPresent(attribute.Name));
                
                if (!ldapToDotNetMetaMap.IsMultiValued)
                {
                    expandoDictionary[attribute.Name] = searchResultParser.GetSingleValuedAttributeData(
                        ldapToDotNetMetaMap,
                        attribute);
                }
                else
                {
                    expandoDictionary[attribute.Name] = searchResultParser.GetMultiValuedAttributeData(
                        ldapToDotNetMetaMap,
                        attribute,
                        searchResultEntry.DistinguishedName);
                }
            }

            return expando;
        }
    }
}
*/